import {
  CMEVENTS,
  contextMenuManager,
  DocumentUtils,
  IEnvironment,
  ObjInfo,
} from '@amcax/base';
import EnvSwitch from '@app-base/common/components/EnvSwitch';
import { eventCenterManager } from '@app-base/common/events/EventCenterManager';
import { dataManager, userEvents } from '@app-base/module';
import { nmAppLog, Renderer } from '@app-nextmesh/common/utils';
import { GspEventType, ModelEventType } from '@app-nextmesh/types';
import { events } from '@app-nextmesh/utils';
import { cloneDeep } from 'lodash';
import { initLoad } from './pages/InitLoad';
import { clearModel } from './utils/renderModel';
import AssistLayer from './widgets/AssistLayer';
import CommandBar from './widgets/CommandBar';
import {
  NextMeshContextMenu,
  NextMeshSelectedContextMenu,
} from './widgets/ContextMenu';
import LeftPanel from './widgets/LeftPanel';
import RightNavigation from './widgets/RightNavigation';
import ToolBar from './widgets/ToolBar';

// 记录DataManagerObjectInfo的数据
let dataManagerObjectInfo: Map<string, ObjInfo> = null;
const init = () => {
  // 这里弃用了，根本没有调用
  nmAppLog('Env Init Nextmesh');
};
const handleModelRightClick = ({
  event,
  data,
}: {
  event: PointerEvent;
  data?: string[];
}) => {
  setTimeout(() => {
    if (data?.length > 0) {
      contextMenuManager.show(event, NextMeshSelectedContextMenu.id, data);
    } else {
      contextMenuManager.show(event, NextMeshContextMenu.id);
    }
  });
};
const load = async () => {
  nmAppLog('Env Load Nextmesh params');

  userEvents.on(CMEVENTS.MODEL_RIGHT_CLICK, handleModelRightClick);

  // 记录DataManagerObjectInfo的数据
  dataManagerObjectInfo = cloneDeep(dataManager.getObjectsInfoMap());

  events.on(ModelEventType.Loading, initLoad.doLoadModel.bind(initLoad));
  events.on(ModelEventType.LoadingField, initLoad.doLoadField.bind(initLoad));
  events.on(
    ModelEventType.SetColormap,
    initLoad.handleSetColormap.bind(initLoad),
  );
  events.on(
    ModelEventType.ClippingChange,
    initLoad.clippingRender.bind(initLoad),
  );
  events.on(GspEventType.shapeChanged, initLoad.handleGspEvent.bind(initLoad));
  events.on(GspEventType.meshChanged, initLoad.handleGspEvent.bind(initLoad));

  eventCenterManager.switchTo('nextmesh');
};
const unload = async () => {
  nmAppLog('Env UnLoad Nextmesh');

  userEvents.off(CMEVENTS.MODEL_RIGHT_CLICK, handleModelRightClick);

  clearModel();
  if (dataManagerObjectInfo != null) {
    // “几何”的渲染数据可能被“网格”的覆盖了，这里重新保存一下
    initLoad.saveShapeDatas();

    // 重新载入备份的模型objectsInfo；因为Render依赖它，渲染“网格”等时会修改它
    dataManager.objectsInfoMap = cloneDeep(dataManagerObjectInfo);

    const modelDxids: string[] = Array.from(dataManagerObjectInfo.keys());
    // 渲染数据已经在Renderer 中了，直接渲染
    await Renderer().drawNewModels(modelDxids);
  }

  events.off(ModelEventType.Loading);
  events.off(ModelEventType.LoadingField);
  events.off(ModelEventType.SetColormap);
  events.off(ModelEventType.ClippingChange);
  events.off(GspEventType.shapeChanged);
  events.off(GspEventType.meshChanged);

  // TODO 释放资源，重置canvas渲染界面等
};
const commandBar = () => {
  return DocumentUtils.isReadOnly() ? <></> : <CommandBar />;
};
const contextMenus = () => {
  return [];
};
const toolBar = () => {
  return DocumentUtils.isReadOnly() ? <></> : <ToolBar />;
};
const leftPanel = () => {
  return <LeftPanel />;
};
const leftTopNavBar = () => {
  return <EnvSwitch />;
};
const rightNavigation = () => {
  return DocumentUtils.isReadOnly() ? <></> : <RightNavigation />;
};
const actionBar = () => {
  return <></>; // 没有ActionBar
};
const assistLayer = () => {
  return <AssistLayer />;
};

export const nextMeshEnvironment: IEnvironment = {
  id: 'Nextmesh',
  label: '仿真',
  index: 2,
  showViewCube: true,
  // showInNavTab: true,
  // showNavTabs: true,
  showCmdLabel: true,
  init,
  load,
  unload,
  commandBar, // 没有CommandBar
  contextMenus,
  toolBar,
  leftTopNavBar,
  leftPanel,
  rightNavigation,
  actionBar,
  assistLayer,
};
